package team.se.tms.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.se.tms.dao.Dao;
import team.se.tms.dto.*;
import team.se.tms.exception.DeleteException;
import team.se.tms.exception.UpdateException;
import team.se.tms.po.ProjectInfo;
import team.se.tms.po.StageInfo;
import team.se.tms.po.TeachCourseInfo;
import team.se.tms.service.StageInfoService;
import team.se.tms.util.KeyUtil;

import java.util.*;

@Service("StageInfoService")
@Transactional(rollbackFor = Exception.class)
public class StageInfoServiceImpl implements StageInfoService {
    @Autowired
    private Dao dao;

    /**
     * 获取项目阶段详情
     *
     * @param projectId 项目id
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2018/12/21 by johnny
     */
    @Override
    public ProjectStageDetail getProjectStageDetail(String projectId) throws Exception {
        return dao.selectOne("Project_StageInfoMapper.selectProjectStageDetailByProjectId",projectId);
    }

    /**
     * 获取所有项目的完成状态
     *
     * @param page 分页
     * @param teachCourseId 授课id
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2018/12/31 by johnny
     */
    @Override
    public List<ProjectStageDetail> listAllProjectStageDetail(Page page, String teachCourseId) throws Exception {
        ParameterPage parameterPage =new ParameterPage();
        parameterPage.setPage(page);
        parameterPage.put("teachCourseId",teachCourseId);
        List<ProjectInfo> projectInfoList =dao.selectList("Project_StageInfoMapper.selectProjectInfoByTeachCourseIds",parameterPage);
        Iterator<ProjectInfo> iterator =projectInfoList.iterator();
        List<ProjectStageDetail> projectStageDetailList =new ArrayList<>();
        while (iterator.hasNext()){
            ProjectInfo projectInfo = iterator.next();
            ProjectStageDetail projectStageDetail = new ProjectStageDetail();
            projectStageDetail = this.getProjectStageDetail(projectInfo.getProjectId());
            projectStageDetailList.add(projectStageDetail);
        }
        return projectStageDetailList;
    }

    /**
     * 修改项目阶段完成状态及分数
     *
     * @param projectId 项目id
     * @param stageId 阶段id
     * @return grade 阶段评分
     * @throws Exception 异常
     * @create on 2018/12/21 by johnny
     */
    @Override
    public Boolean modifyProjectStageComplete(String projectId, String stageId,Float grade) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("projectId",projectId);
        map.put("stageId",stageId);
        map.put("grade",grade);
        map.put("result",-1);
        dao.update("Project_StageInfoMapper.updateProjectStageCompleteByProjectIdAndStageId",map);
        if((int)map.get("result")==0){
            throw new UpdateException("上个阶段尚未完成");
        }
        if((int)map.get("result")==2){
            throw new UpdateException("--分数应为1-100的数字");
        }
        if((int)map.get("result")==-1){
            throw new UpdateException();
        }

        return true;
    }
    /**
     * 添加阶段链
     *
     * @param stageInfos 阶段信息
     * @return Boolean 添加结果
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
    @Override
    public Boolean saveStageChain(List<StageInfo> stageInfos) throws Exception {
        //给stageInfos设置主键
        if(stageInfos!=null && stageInfos.size()>0){
            for(StageInfo stageInfo:stageInfos){
                stageInfo.setStageId(KeyUtil.generatePrimaryKey());
            }
        }
        List<StageDetail> stageDetailList = new ArrayList<>();
        if(stageInfos.size()==1){
            //如果只有一个阶段
            stageDetailList.add(new StageDetail(stageInfos.get(0).getStageId(),stageInfos.get(0).getStageName(),null,null));
        }else{
            for(int i=0;i<stageInfos.size();i++){
                StageDetail stageDetail = new StageDetail();
                stageDetail.setStageId(stageInfos.get(i).getStageId());
                stageDetail.setStageName(stageInfos.get(i).getStageName());
                if(i==0){
                    //如果为第一个阶段,则上一个阶段为null
                    stageDetail.setStagePreId(null);
                    stageDetail.setStageNextId(stageInfos.get(i+1).getStageId());
                }else if(i==stageInfos.size()-1){
                    //如果为最后一个阶段,则下一个阶段为null
                    stageDetail.setStagePreId(stageInfos.get(i-1).getStageId());
                    stageDetail.setStageNextId(null);
                }else {
                    stageDetail.setStagePreId(stageInfos.get(i-1).getStageId());
                    stageDetail.setStageNextId(stageInfos.get(i+1).getStageId());
                }
                stageDetailList.add(stageDetail);
            }
        }
        if(dao.insert("StageInfoMapper.insertStageInfos",stageDetailList)!=0){
            return true;
        }
        return false;
    }
    /**
     * 查询所有的阶段链
     *
     * @param page
     * @return List<StageChain>
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
    @Override
    public List<StageChain> listStageChains(Page page) throws Exception {
        List<StageChain> stageChainList =dao.selectList("StageInfoMapper.selectAllFirstStageId",page);
        return stageChainList;
    }
    /**
     * 修改阶段链
     *
     * @param oldLineFirstStageId 修改的阶段链第一个极端id
     * @param stageInfos 阶段信息
     * @return Boolean 修改结果
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
    //需要事务
    @Override
    public Boolean modifyStageChain(String oldLineFirstStageId,List<StageInfo> stageInfos) throws Exception {

        if(deleteStageChain(oldLineFirstStageId)){
            if(this.saveStageChain(stageInfos)){
                return true;
            }
        }
        return false;
    }

    /**
     * @update on 2019/1/2 by argentum
     * @param firstStageId
     * @return
     * @throws Exception
     */
    @Override
    public Boolean deleteStageChain(String firstStageId) throws Exception {
        //判断阶段链是否被使用

        //删除阶段链
        if(dao.delete("StageInfoMapper.delectStageChainByFirstStageId",firstStageId)>0){
            return true;
        }
        return false;
    }

    /**
     * 删除阶段链
     * @create on 2018/12/30 by argentum
     *
     * @param stageId
     * @return
     * @throws Exception
     */
    @Override
    public Boolean removeStageInfos(String stageId) throws Exception {
        List<TeachCourseInfo> teachCourseInfos = dao.selectList("TeachCourseInfoMapper.selectTeachCourseInfosByStageId",stageId);
        if(teachCourseInfos==null)
        {
            int count  = dao.delete("StageInfoMapper.delectStageChainByFirstStageId",stageId);
            if(count>0){
            return true;
        }
        }else{
            throw new DeleteException("正在被使用");
        }
        return false;
    }

}
